﻿package com.tudou.ad.components
{
    import com.tudou.utils.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import gs.*;

    public class AdDoor extends Sprite
    {
        private var left:Bitmap;
        private var showWidth:Number = 0;
        private var right:Bitmap;
        public static const OPEN_SHOWED:String = "ad door open";
        public static const CLOSE_SHOWED:String = "ad door close";

        public function AdDoor(param1:BitmapData)
        {
            if (param1 == null)
            {
                return;
            }
            showWidth = param1.width;
            left = generateBmp(param1, 0, 0, showWidth / 2, param1.height);
            left.x = 0;
            left.y = 0;
            addChild(left);
            right = generateBmp(param1, showWidth / 2, 0, showWidth / 2, param1.height);
            right.x = showWidth / 2;
            right.y = 0;
            addChild(right);
            RectDisplayUtil.drawRect(this.graphics, showWidth, param1.height, 0, 0);
            var _loc_2:* = RectDisplayUtil.getShape(showWidth, param1.height);
            addChild(_loc_2);
            this.mask = _loc_2;
            this.useHandCursor = true;
            this.buttonMode = true;
            return;
        }// end function

        private function closedHlr() : void
        {
            this.dispatchEvent(new Event(CLOSE_SHOWED));
            return;
        }// end function

        private function updateHlr() : void
        {
            right.x = showWidth / 2 - left.x;
            return;
        }// end function

        public function closeDoor() : void
        {
            left.x = (-showWidth) / 2;
            right.x = showWidth;
            TweenLite.killTweensOf(left);
            TweenLite.to(left, 1, {x:0, onUpdate:updateHlr, onComplete:closedHlr});
            return;
        }// end function

        private function openedHlr() : void
        {
            this.dispatchEvent(new Event(OPEN_SHOWED));
            return;
        }// end function

        private function generateBmp(param1:BitmapData, param2:int, param3:int, param4:int, param5:int) : Bitmap
        {
            var _loc_6:* = new BitmapData(param4, param5);
            new BitmapData(param4, param5).copyPixels(param1, new Rectangle(param2, param3, param4, param5), new Point());
            return new Bitmap(_loc_6);
        }// end function

        public function stopDoor() : void
        {
            this.visible = false;
            TweenLite.killTweensOf(left, true);
            return;
        }// end function

        public function openDoor() : void
        {
            left.x = 0;
            right.x = showWidth / 2;
            TweenLite.killTweensOf(left);
            TweenLite.to(left, 1, {x:(-showWidth) / 2, onUpdate:updateHlr, onComplete:openedHlr});
            return;
        }// end function

    }
}
